﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using Besture.Bll;
using Besture.Common;
using Besture.Model;
using Besture.Utils;
using Besture.Common.Definition;

namespace Besture.UI.Admin.News
{
    public partial class ActiveNewsSet : BasePage
    {
        public string _txtNewsContext = string.Empty;//存放内容的多文本值
        public string _txtKeyWords = string.Empty;//存放副标题的多文本值

        #region 页面加载事件

        /// <summary>
        /// 页面加载
        /// </summary>
        protected override void InitialLoad()
        {
            if (!IsPostBack)
            {
                bindClassID();

                if (Request.QueryString["id"] != null && !string.IsNullOrEmpty(Request.QueryString["id"]))
                {
                    //编辑编号
                    hidEditID.Value = Request.QueryString["id"];
                    if (!Fun.IsNumber(hidEditID.Value))
                    {
                        RedirectErrorUrl("编号错误");
                    }
                    InitData(Convert.ToInt32(hidEditID.Value));
                }
            }
        }

        /// <summary>
        /// 回发
        /// </summary>
        protected override void PostBackLoad()
        {

        }

        /// <summary>
        /// 数据加载
        /// </summary>
        /// <param name="id">编号</param>
        protected void InitData(int id)
        {
            ActiveNewsDac dac = new ActiveNewsDac();
            ActiveNewsModel model = dac.GetModel(id);
            ddlClassID.SelectedValue = model.ClassID.ToString();
            txtTitle.Text = model.Title != null ? model.Title.ToString() : "";
            txtSource.Text = model.Source != null ? model.Source.ToString() : "";
            _txtKeyWords = model.KeyWords != null ? model.KeyWords.ToString() : "";
            txtSummary.Text = model.Summary != null ? model.Summary.ToString() : "";
            txtPubDate.Text = model.PubDate.HasValue ? model.PubDate.Value.ToString("yyyy-MM-dd") : "";
            UploadPhotoImageUrl.ImageUrl = model.ImageUrl != null ? model.ImageUrl.ToString() : "";
            _txtNewsContext = model.NewsContext != null ? model.NewsContext.ToString() : "";
            rbtnIsHome.SelectedValue = model.IsHome.ToString();

        }
        #endregion

        #region 保存

        protected void btnSubmit_Click(object sender, EventArgs eventArgs)
        {
            ActiveNewsDac dac = new ActiveNewsDac();
            ActiveNewsModel model = null;
            string editID = hidEditID.Value;
            //添加
            if (string.IsNullOrEmpty(editID))
            {
                model = new ActiveNewsModel();
                model.ClassID = ddlClassID.SelectedValue != "" ? Convert.ToInt32(ddlClassID.SelectedValue) : 0;
                model.Title = txtTitle.Text.Trim();
                model.Source = txtSource.Text.Trim();
                model.KeyWords = Request.Params["editorKeyWords"] != null ? Request.Params["editorKeyWords"].ToString() : "";
                model.Summary = txtSummary.Text.Trim();
                model.PubDate = string.IsNullOrEmpty(txtPubDate.Text.Trim()) ? DateTime.Now : Convert.ToDateTime(txtPubDate.Text.Trim());
                model.ImageUrl = UploadPhotoImageUrl.ImageUrl;
                model.NewsContext = Request.Params["editorNewsContext"] != null ? Request.Params["editorNewsContext"].ToString() : "";
                model.IsHome = rbtnIsHome.SelectedValue != "" ? Convert.ToInt32(rbtnIsHome.SelectedValue) : 0;
                model.CreateBy = LoginUserInfo.ID;
                model.CreateTime = DateTime.Now;
                model.UpdateBy = LoginUserInfo.ID;
                model.UpdateTime = DateTime.Now;
                model.IsDeleted = 0;
                dac.Add(model);
            }
            //编辑
            else
            {
                model = dac.GetModel(Convert.ToInt32(editID));
                model.ClassID = ddlClassID.SelectedValue != "" ? Convert.ToInt32(ddlClassID.SelectedValue) : 0;
                model.Title = txtTitle.Text.Trim();
                model.Source = txtSource.Text.Trim();
                model.KeyWords = Request.Params["editorKeyWords"] != null ? Request.Params["editorKeyWords"].ToString() : "";
                model.Summary = txtSummary.Text.Trim();
                model.PubDate = string.IsNullOrEmpty(txtPubDate.Text.Trim()) ? DateTime.Now : Convert.ToDateTime(txtPubDate.Text.Trim());
                model.ImageUrl = UploadPhotoImageUrl.ImageUrl;
                model.NewsContext = Request.Params["editorNewsContext"] != null ? Request.Params["editorNewsContext"].ToString() : "";
                model.IsHome = rbtnIsHome.SelectedValue != "" ? Convert.ToInt32(rbtnIsHome.SelectedValue) : 0;
                model.CreateBy = LoginUserInfo.ID;
                model.CreateTime = DateTime.Now;
                model.UpdateBy = LoginUserInfo.ID;
                model.UpdateTime = DateTime.Now;
                model.IsDeleted = 0;
                dac.Update(model);
            }
            Fun.Alert("保存成功", "ActiveNewsList.aspx");
        }
        #endregion

        #region 取消

        public void btnCanel_Click(object sender, EventArgs eventArgs)
        {
            RedirectUrl("ActiveNewsList.aspx");
        }

        #endregion

        /// <summary>
        ///绑定类型
        /// </summary>
        public void bindClassID()
        {
            ActiveNewsClassDac dac = new ActiveNewsClassDac();
            DataTable dt = dac.GetDataTable(Constants.DICTIONARY_Code_NewsClassNew);
            this.ddlClassID.Items.Add(new ListItem("请选择", ""));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                this.ddlClassID.Items.Add(new ListItem(dt.Rows[i]["NewsClassName"].ToString(), dt.Rows[i]["ID"].ToString()));
            }
        }

    }
}
